import { getReport, selectItem } from '@/services/goods/report';

const group = {
  namespace: 'qualityreport',
  state: {
    page: 1,
    pageSize: 20,
    searchParams: {},
    total: 0,
    tableData: [],
    addOpen: false,
    drawerParams: {},
  },
  reducers: {
    updateState: (state, { payload }) => ({
      ...state,
      ...payload,
    }),
  },
  effects: {
    *selectPage({ payload }, { call, put }) {
      const { code, result, message } = yield call(getReport, payload);
      let tableData = [];
      if (code === 200 && result) {
        //
        tableData = result?.records || [];
      }
      yield put({
        type: 'updateState',
        payload: {
          tableData,
          message,
          total: result?.total || 0,
        },
      });
    },

    *selectItem({ payload }, { call, put }) {
      const { id, spuCode } = payload;
      const { code, data } = yield call(selectItem, { id, spuCode });
      if (code === 200) {
        yield put({
          type: 'updateState',
          payload: {
            addOpen: true,
            drawerParams: data,
          },
        });
      }
    },
  },
};

export default group;
